<route>{meta: {title: '亲子频道banner'}}</route>
<template>
  <Page scroll>
    <el-form ref="form" label-width="80px">
      <el-form-item label-width="0">
        <el-button type="primary" @click="add()">新增</el-button>
        <el-button type="primary" @click="download()">保存下载</el-button>
      </el-form-item>
      <div class="section" v-for="(item, index) in list" :key="item.bannerImageId">
        <el-form-item label="标题:">
          <el-input v-model="item.title"></el-input>
        </el-form-item>
        <el-form-item label="图片地址">
          <el-input v-model="item.imageUrl"></el-input>
        </el-form-item>
        <el-form-item :label="item.jumpType === '1' ? '跳转地址' : '跳转id'">
          <el-input v-model="item.forwardUrl"></el-input>
        </el-form-item>
        <el-form-item label="跳转类型">
          <el-select v-model="item.jumpType">
            <el-option label="链接" value="1"></el-option>
            <el-option label="纸书详情" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <div style="text-align: right;width: 100%;">
            <el-button type="danger" @click="remove(index)">删除</el-button>
          </div>
        </el-form-item>
      </div>
    </el-form>
  </Page>
</template>

<script>
/**
 Author: 蒋鑫强
 Time: 2023/8/1
 Description:
 */
import Page from '@/components/Page';
import file from '@/native/file';
import dialog from '@/util/dialog';

export default {
  name: 'parentingData',

  components: {
    Page
  },

  data() {
    return {
      list: []
    };
  },

  methods: {
    async getList() {
      const funcText = await file.readFile('tmp/parenting-data.js');
      const func = new Function(funcText);
      func();
      this.list = window.parentingData.banner;
    },

    remove(index) {
      dialog.confirm('确认删除？').then(() => {
        this.list.splice(index, 1);
      });
    },

    add() {
      this.list.unshift({
        imageUrl: '',
        forwardUrl: '',
        jumpType: '1',
        title: '',
        bannerImageId: Math.random() * 1000 + 9000
      });
    },

    download() {
      const funcTextObj = {
        banner: this.list
      };
      const funcText = `window.parentingData = ${JSON.stringify(funcTextObj)}`;
      file.writeFile('tmp/parenting-data.js', funcText).then(() => {
        file.downloadFile({
          url: 'tmp/parenting-data.js',
          name: 'parenting-data.js'
        });
      });
    }
  },

  created() {
    this.getList();
  }
};
</script>

<style lang="less" scoped>
.section {
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-bottom: 10px;
  padding: 10px;
}
</style>
